import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ILike } from 'typeorm';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
import { RoleService } from './role.service';
import {
  CreateRoleDto,
  FindRoleListDto,
  RoleDto,
  UpdateRoleDto,
} from './role.dto';
@ApiTags('角色模块')
@ApiBearerAuth()
@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @ApiOperation({ summary: '查询角色列表' })
  @ApiResponse({
    status: 200,
    description: '查询角色列表',
    type: RoleDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:role:list')
  async findList(
    @Query() dto: FindRoleListDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.roleService.getLists({
      where: {
        ...dto,
        role_name: dto.role_name ? ILike('%' + dto.role_name + '%') : null,
        is_delete: '0',
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询所有角色' })
  @ApiResponse({
    status: 200,
    description: '查询所有角色',
    type: RoleDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get('findAll')
  async findAll(@Headers() headers): Promise<IResponseData> {
    const res = await this.roleService.getAll({
      where: {
        is_delete: '0',
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询角色详情' })
  @ApiResponse({
    status: 200,
    description: '查询角色详情',
    type: RoleDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:role:detail')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.roleService.getOne({
      where: {
        id,
      },
      relations: ['menus'],
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:role:add')
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreateRoleDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.roleService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:role:update')
  @ApiOperation({ summary: '更新角色' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdateRoleDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.roleService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:role:delete')
  @ApiOperation({ summary: '删除角色' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.roleService.update_delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
